clear;clc;

%% 调用CSTStudio并初始化
% A = img(:,:,n); % 下层图案
% a = img(:,:,n+2); % 上层图案
path = pwd; % 获取当前m文件夹路径
cst = actxserver('CSTStudio.application');
mws = invoke(cst, 'NewMWS');
app = invoke(mws, 'GetApplicationName');
ver = invoke(mws, 'GetApplicationVersion');
invoke(mws, 'FileNew');
invoke(mws, 'DeleteResults'); % 删除之前的结果，防止程序停止运行
tic;
CST_Modeling(mws);
disp(["建模成功，所用时间为：", num2str(toc)]);
% invoke(invoke(mws,'FDSolver'),'Start');
function [] = CST_Modeling(mws)

    Frq = [0.2, 1]; % 工作频率
    Property_Setup(mws, Frq);
    %% 仿真参数设置
    Px = 400; %结构X向边长
    Py = Px; %结构Y向边长
    t_metal = 0.2; %基地层金属厚度
    t_dielectric = 30;
    R1 = 100;
    R2 = 50;
    W = 50;
    % 在CST中加入结构参数，方便后续查看
    invoke(mws, 'StoreParameter', 'Px', Px);
    invoke(mws, 'StoreParameter', 'Py', Py);
    invoke(mws, 'StoreParameter', 't_metal', t_metal);
    invoke(mws, 'StoreParameter', 't_dielectric', t_dielectric);
    invoke(mws, 'StoreParameter', 'R1', R1);
    invoke(mws, 'StoreParameter', 'R2', R2);
    invoke(mws, 'StoreParameter', 'W', W);

    %% 组件
    sCommand = '';
    sCommand = [sCommand 'Component.New "Dielectric"'];
    invoke(mws, 'AddToHistory', 'New Component Dielectric', sCommand);
    sCommand = '';
    sCommand = [sCommand 'Component.New "Metal"'];
    invoke(mws, 'AddToHistory', 'New Component Metal', sCommand);

    %% 建立模型
    %% 1. 建立底层金属模型
    sCommand = '';
    sCommand = [sCommand 'With Brick'];
    sCommand = [sCommand 10 '    .Reset'];
    sCommand = [sCommand 10 '    .Name "基底"'];
    sCommand = [sCommand 10 '    .Component "Metal"'];
    sCommand = [sCommand 10 '    .Material "Gold"'];
    sCommand = [sCommand 10 '    .Xrange "-Px/2", "Px/2"'];
    sCommand = [sCommand 10 '    .Yrange "-Py/2", "Py/2"'];
    sCommand = [sCommand 10 '    .Zrange "-t_metal", "0"'];
    sCommand = [sCommand 10 '    .Create'];
    sCommand = [sCommand 10 'End With'];
    invoke(mws, 'AddToHistory', '定义块:基底', sCommand);
    %% 2. 建立介质层模型

    Model_Brick(mws, '-Px/2', 'Px/2', '-W/2', 'W/2', '0', 't_dielectric', 'HTL_YW', 'Dielectric', '介质结构1');

    Model_Brick(mws, '-W/2', 'W/2', '-Py/2', 'Py/2', '0', 't_dielectric', 'HTL_YW', 'Dielectric', '介质结构2');

    %
    %         solid = invoke(mws,'Solid');
    %         invoke(solid,'Add',['Dielectric:介质结构1'],['Dielectric:介质结构1']);
    %         release(solid);
    invoke(mws, 'AddToHistory', '合并以生成十字结构', 'Solid.Add "Dielectric:介质结构1", "Dielectric:介质结构2" ');

    %% 3 创建上层金属
    %{
    sCommand = '';
    sCommand = [sCommand 'With Cylinder'];
    sCommand = [sCommand 10 '.Reset'];
    sCommand = [sCommand 10 '    .Name "上层金属"'];
    sCommand = [sCommand 10 '    .Component "Metal"'];
    sCommand = [sCommand 10 '    .Material "Gold"'];
    sCommand = [sCommand 10 '    .OuterRadius "R1"'];
    sCommand = [sCommand 10 '    .InnerRadius "R2"'];
    sCommand = [sCommand 10 '    .Axis "z"'];
    sCommand = [sCommand 10 '    .Zrange "t_dielectric", "t_dielectric+t_metal"'];
    sCommand = [sCommand 10 '    .Xcenter "0"'];
    sCommand = [sCommand 10 '    .Ycenter "0"'];
    sCommand = [sCommand 10 '    .Segments "0"'];
    sCommand = [sCommand 10 '    .Create'];
    sCommand = [sCommand 10 'End With'];
    invoke(mws, 'AddToHistory', '定义上层金属', sCommand);
    %}
    %% 后处理
    %{
    CST_Post = CST_MicrowaveStudio();
    [freq, sparam, stype] = CST_Post.getSParameters;
    % 得到的S参数依次为：
    % Szmax1,zmax1    Szmax2,zmax1    Szmin1,zmax1    Szmin2,max1
    % Szmax1,zmax2    Szmax2,zmax2    Szmin1,zmax2    Szmin2,zmax2
    % Szmax1,zmin1     Szmax2,zmin1     Szmin1,zmin1    Szmin2,zmin1
    % Szmax1,zmin2     Szmax2,zmin2    Szmin1,zmin2    Szmin2,zmin2
    % Szmax1,zmax1 == Szmax2,zmax2
    % 取CD = Szmax2,zmax1-Szmax1,zmax2
    % frequency = freq(:,1); % 获取频率点
    % save('frequency.mat','frequency');
    real = real(sparam); % 获取实部
    imag = imag(sparam); % 获取虚部
    linear = sqrt(real .* real + imag .* imag); % 计算linear值
    Szmax1zmax1 = linear(:, 1);
    Szmax2zmax1 = linear(:, 2);
    Szmax1zmax2 = linear(:, 5);
    CD = Szmax2zmax1 .* Szmax2zmax1 - Szmax1zmax2 .* Szmax1zmax2;
    real_name = [path, '\result\real\real_', num2str(n), '.mat'];
    imag_name = [path, '\result\imag\imag_', num2str(n), '.mat'];
    Szmax1zmax1_name = [path, '\result\S-Parameters\Szmax1zmax1_', num2str(n), '.mat'];
    Szmax2zmax1_name = [path, '\result\S-Parameters\Szmax2zmax1_', num2str(n), '.mat'];
    Szmax1zmax2_name = [path, '\result\S-Parameters\Szmax1zmax2_', num2str(n), '.mat'];
    CD_name = [path, '\result\CD\CD_', num2str(n), '.mat'];
    save(real_name, 'real');
    save(imag_name, 'imag');
    save(Szmax1zmax1_name, 'Szmax1zmax1');
    save(Szmax2zmax1_name, 'Szmax2zmax1');
    save(Szmax1zmax2_name, 'Szmax1zmax2');
    save(CD_name, 'CD')
    clear freq sparam stype real imag linear Szmax1zmax1 Szmax2zmax1 Szmax1zmax2 CD
    % 保存并退出
    % invoke(mws,'Save');
    filename = ['\metamaterials\metamaterial', num2str(n), '.cst']; % 新建的CST文件名字
    fullname = [path filename];
    invoke(mws, 'SaveAs', fullname, 'True'); % 保存到目前为止的结果
    invoke(mws, 'Quit');
%}

end
